using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Deepcode.Flux.Core.Extensions;
using Deepcode.Flux.Core.Systems.Registry;
using Deepcode.Flux.Core.UI.Controls;
using Deepcode.Flux.Core.Systems.UI.Taskpad;

public partial class Menu_taskpad : Deepcode.Flux.Core.UI.AdministrationModulePage
{
	protected void Page_Load(object sender, EventArgs e)
	{
		RecreateTaskpads();
	}

	#region Population of the taskpads
	private FluxRegistry RegistryCollapsed = null;
	private FluxRegistry RegistryRemoved = null;

	/// <summary>
	/// Recreates all of the tabs and taskpad options
	/// </summary>
	private void RecreateTaskpads()
	{
		// Going to use registry, create it
		RegistryCollapsed = new FluxRegistry(FluxRegistryRoot.CURRENT_USER, "/Taskpad/Collapsed");
		RegistryRemoved = new FluxRegistry(FluxRegistryRoot.CURRENT_USER, "/Taskpad/Removed");
		
		// Get tabs from addin tree
		AddinTreeNode root = AddinTree.Tree.GetTreeNodeNoThrow("/Flux/Taskpad/");
		if (root != null)
		{				
			// Get all of the taskpad tabs
			TaskpadTab[] tabs = root.BuildChildItems(this).ToArray(typeof(TaskpadTab)) as TaskpadTab[];
			
			// Create each one
			foreach (TaskpadTab tab in tabs)
				CreateTabAndControls(tab);
		}
		// If we have no active tab, ensure first one is selected
		if (!Page.IsPostBack)
			TaskpadTabstrip.ActivateFirstTab();
	}
	/// <summary>
	/// Creates one tab, adds it to the tab control. Also creates the
	/// container area for the ASCX's and sections to be loaded into and
	/// invokes the loader for each custom control.
	/// </summary>
	/// <param name="tab"></param>
	private void CreateTabAndControls(TaskpadTab tab)
	{
		// Create the tab host control
		TabContainer ParentContainer = new TabContainer();
		ParentContainer.ID = tab.ID + "_host";
		TaskpadHost.Controls.Add(ParentContainer);

		int iCount = 0;
		// Now create each of the pads - counting each one created
		foreach (TaskpadControl ControlDef in tab.Options)
			if( CreateSectionAndASCX(ControlDef, ParentContainer) ) iCount ++;

		// Create the tab if it has a control (only non postback, - tabs are stored in viewstate)
		if (!Page.IsPostBack && iCount > 0)
		{
			Tab t = new Tab();
			t.Title = tab.Title;
			t.ContainerID = tab.ID + "_host";
			TaskpadTabstrip.Tabs.Add(t);
		}

	}
	/// <summary>
	/// Creates a collapse section hoster and loads the ASCX control into it.
	/// </summary>
	/// <param name="c"></param>
	/// <param name="tc"></param>
	private bool CreateSectionAndASCX(TaskpadControl ControlDef, TabContainer ParentContainer)
	{
		// Ensure the user hasn't already removed this control via my profile
		if( RegistryRemoved.GetKey( ControlDef.ID, false )) return false;
		// Create the section and add it to the container	
		CollapseSection section = new CollapseSection();
		section.Title = ControlDef.Title;
		section.ID = ControlDef.ID + "_section";
		section.Tag = ControlDef.ID;
		section.AutoPostback = true;
		section.ExpandCollapse += new CollapseSection.OnExpandCollapse(TaskpadSectionExpandCollapse);
		section.Expanded = !RegistryCollapsed.GetKey(ControlDef.ID, false);

		ParentContainer.Controls.Add(section);
		// Attempt to load and display the ASCX
		try
		{
			Control HostASCX = LoadControl(ControlDef.ASCX);
			section.Controls.Add(HostASCX);
		}
		catch(Exception e)
		{
			Literal ErrorText = new Literal();
			ErrorText.Text = "<p style='padding:0px 10px 0px 10px; color:red; font-weight:bold;'>";
			ErrorText.Text += "Could not load control " + ControlDef.ASCX;
			ErrorText.Text += "</p>";
			section.Controls.Add(ErrorText);

			Deepcode.Flux.Core.Systems.Audit.Quick.QuickAudit.RaiseError(
				new Deepcode.Flux.Core.Systems.Audit.Quick.AuditUser(),
				"Flux",
				"Taskpad",
				"Could not load control {0} ({1})",
				ControlDef.ASCX,
				e.Message);
		}
		return true;
	}
	#endregion
	#region Section expand / collapse code
	/// <summary>
	/// Track open / closed sections
	/// </summary>
	/// <param name="source"></param>
	private void TaskpadSectionExpandCollapse(CollapseSection source)
	{
		FluxRegistry reg = new FluxRegistry(FluxRegistryRoot.CURRENT_USER, "/Taskpad/Collapsed");
		if (!source.Expanded) reg.SetKey(source.Tag.ToString(), true);
		else reg.DeleteKey(source.Tag.ToString());
	}
	#endregion
}
